#include <stdio.h>
#include <string.h>
char s[27], c[27];
int m, l;
int C(int a, int b)
{
    long long ans = 1;
    for (int i = a - b + 1; i <= a; i++)
        ans *= i;
    for (int i = 2; i <= b; i++)
        ans /= i;
    return (int)ans;
}
int f(int l, char *s, char *c)
{
    if (!l)
        return 1;
    int cur = 0, num = 0, ans = 1;
    while (cur < l)
    {
        for (int i = cur; i < l; i++)
            if (c[i] == s[cur])
            {
                num++;
                ans *= f(i - cur, s + cur + 1, c + cur);
                cur = i + 1;
                break;
            }
    }
    return ans *= C(m, num);
}
int main()
{
    while (scanf("%d", &m), m)
    {
        scanf("%s%s", s, c);
        int l = strlen(s);
        printf("%d\n", f(l - 1, s + 1, c));
    }
    return 0;
}